package so.shanku.essential.activity;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.*;
import aym.util.getservicesdata.GetServicesDataQueue;
import aym.util.getservicesdata.GetServicesDataUtil;
import aym.util.json.JsonMap;
import aym.util.json.JsonParseHelper;
import com.nostra13.universalimageloader.core.ImageLoader;
import net.tsz.afinal.annotation.view.ViewInject;
import so.shanku.essential.R;
import so.shanku.essential.activity.base.BaseUIActivity;
import so.shanku.essential.adapter.ShopIntroductAdapter;
import so.shanku.essential.app.MyApplication;
import so.shanku.essential.getdata.GetDataConfing;
import so.shanku.essential.getdata.GetDataQueue;
import so.shanku.essential.getdata.JsonKeys;
import so.shanku.essential.getdata.ShowGetDataError;
import so.shanku.essential.utils.ExtraKeys;
import so.shanku.essential.utils.ImageLoaderBitmapCallBack;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2015/9/25.
 */
public class ShopIntroductionActivity extends BaseUIActivity implements ShopIntroductAdapter.TelPhoneClick {
	/**
	 * 微博
	 */
	@ViewInject(id = R.id.iv_weibo,click = "iv_weibo_click")
	private ImageView iv_weibo;
	/**
	 * 微信
	 */
	@ViewInject(id = R.id.iv_weichat,click = "iv_weichat_click")
	private ImageView iv_weichat;
	/**
	 * to头部图片
	 */
	@ViewInject(id = R.id.iv_shopintroduct)
	private ImageView iv_shopintroduct;
	/**
	 * 头部信息
	 */
	@ViewInject(id = R.id.tv_shopintroduct)
	private TextView tv_shopintroduct;
	/**
	 * 网址
	 */
	@ViewInject(id = R.id.tv_internet, click = "tv_internet_click")
	private TextView tv_internet;
	/**
	 * 获取更多品牌（新浪 微信图标）
	 */
	@ViewInject(id = R.id.linear_more_msg)
	private LinearLayout linear_more_msg;
	/**
	 * 店铺信息的listview
	 */
	@ViewInject(id = R.id.listview_shopintroduction)
	private ListView listview_shopintroduction;
	private ShopIntroductAdapter shopIntroductAdapter;
	private String shopId;//店铺id
	private JsonMap<String, Object> jsonMap;//店铺介绍数据

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_shop_introduction);
		shopId = getIntent().getStringExtra(ExtraKeys.Key_Msg1);
		getData_ShopIntroduct();
	}

	/**
	 * 打开浏览器
	 * @param internetUrl
	 */
	public void gotoInternet(String internetUrl) {
		Intent intent = new Intent();
		intent.setAction(Intent.ACTION_VIEW);
		intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
		Uri content_url = Uri.parse(internetUrl);
		intent.setData(content_url);
		startActivity(intent);
	}

	/**
	 * 进入店铺网址
	 * @param view
	 */
	public void tv_internet_click(View view) {
//		gotoInternet("http://www.baidu.com");

		gotoInternet(jsonMap.getStringNoNull("CompanyWebseit"));
	}

	/**
	 * 进入店铺微博
	 * @param view
	 */
	public void iv_weibo_click(View view) {
		gotoInternet(jsonMap.getStringNoNull("WeiBo"));
	}

	/**
	 * 进入店铺微信
	 */
	public void iv_weichat_click(View view) {
		String weichat= jsonMap.getStringNoNull("WeChat");
		if(!TextUtils.isEmpty(weichat)){
			showPopView(weichat);
		}
	}

	public void getData_ShopIntroduct() {
		loadingToast.show();
		Map<String, Object> params = new HashMap<String, Object>();
		params.put(GetDataQueue.Params_key, GetDataConfing.Key_Str);
		params.put("VendorId",shopId);
		GetDataQueue queue = new GetDataQueue();
		queue.setCallBack(callBack);
		queue.setActionName(GetDataConfing.GetAction_GetVendorDescribeVO);
		queue.setWhat(GetDataConfing.what_GetVendorDescribeVO);
		queue.setParamsNoJson(params);
		getDataUtil.getData(queue);
	}


	/**
	 * 获取服务器数据的返回信息
	 */
	private GetServicesDataUtil.IGetServicesDataCallBack callBack = new GetServicesDataUtil.IGetServicesDataCallBack() {
		@Override
		public void onLoaded(GetServicesDataQueue entity) {
			if (entity.isOk()) {
				if (ShowGetDataError.isOkAndCodeIsNot1(ShopIntroductionActivity.this,
						entity.getInfo())) {
					if (entity.what == GetDataConfing.what_GetVendorDescribeVO) {
						List<JsonMap<String, Object>> data = JsonParseHelper.getJsonMap_List_JsonMap(
								entity.getInfo(), JsonKeys.Key_Info);
						jsonMap=data.get(0);
						setIntroductData(jsonMap);

					}
				}
			} else {
				ShowGetDataError.showNetError(ShopIntroductionActivity.this);
			}
			loadingToast.dismiss();
		}
	};
	public void  setIntroductData(JsonMap data){
		String brandLogo= data.getStringNoNull("brandLogo");

		if (MyApplication.getInstance().getBitmapHashMap().containsKey(brandLogo)) {
			iv_shopintroduct.setImageBitmap((MyApplication.getInstance().getBitmapHashMap().get(brandLogo)));
		} else {
			ImageLoader.getInstance().displayImage(brandLogo,iv_shopintroduct, MyApplication.getInstance().getDefaultOption(), new ImageLoaderBitmapCallBack());
		}
		tv_shopintroduct.setText(data.getStringNoNull("VendorInfo"));
		tv_internet.setText(data.getStringNoNull("CompanyWebseit"));
		shopIntroductAdapter =new ShopIntroductAdapter(this,data.getList_JsonMap("OfflineStoreList"));
		listview_shopintroduction.setAdapter(shopIntroductAdapter);
		shopIntroductAdapter.setTelPhoneClick(this);

	}

	public PopupWindow popupWindow;
	private TextView close_tv;
	private ImageView weChatCode;
	private void showPopView(String weichatStr) {
		if (popupWindow == null) {
			View view = getLayoutInflater().inflate(R.layout.weichat_code_layout, null);
			popupWindow = new PopupWindow(view,
					ViewGroup.LayoutParams.MATCH_PARENT,
					ViewGroup.LayoutParams.MATCH_PARENT);
			popupWindow.setContentView(view);

            weChatCode = (ImageView) view.findViewById(R.id.weChat_code_iv);
			if (MyApplication.getInstance().getBitmapHashMap().containsKey(weichatStr)) {
				weChatCode.setImageBitmap((MyApplication.getInstance().getBitmapHashMap().get(weichatStr)));
			} else {
				ImageLoader.getInstance().displayImage(weichatStr, weChatCode,MyApplication.getInstance().getDefaultOption(),new ImageLoaderBitmapCallBack());
			}
			popupWindow.setAnimationStyle(R.style.popupAnimation);

			close_tv = (TextView) view
					.findViewById(R.id.close_tv);

			close_tv.setOnClickListener(new View.OnClickListener() {
				@Override
				public void onClick(View view) {
					popupWindow.dismiss();
				}
			});

			popupWindow.showAtLocation(iv_weibo, Gravity.NO_GRAVITY, 0, 0);

		} else if (!popupWindow.isShowing()) {
			popupWindow.showAtLocation(iv_weibo, Gravity.NO_GRAVITY, 0, 0);
		}
	}

	@Override
	public void telPhoneClick(String phone) {
		startTelPhone(phone);
	}
}